<?php
use OSS\OssClient;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/Lib/Base.php';
require_once __DIR__ . '/funcs.php';
require_once __DIR__ . '/Lib/Page.php';

checkLogin();
checkFunc(3);

$now = time();
$success = $_GET['success'] ?? null;
$error = $_GET['error'] ?? null;
$action = $_GET['action'] ?? null;
$search_type = $_POST['search_type'] ?? $_GET['search_type'] ?? null;

$redis_exchange_type = $REDIS->get('EXCHANGE_TYPE');

if($action) {
    if ($action == 'del') {
        checkFunc(3, '权限不足，无法操作');
        $aid = $_GET['id'] ?? null;
        if (!$aid) {
            $error = '无效的类型ID';
        } else {
            $DB->delete('exchange_type_children', ['id' => $aid]);
            $success = '删除成功';
            operate_log('exchange_type_children', 'delete_type_children', ['类型子类ID' => $aid]);

        }
    }
    else if($action == 'add_children') {
        checkFunc(3, '权限不足，无法操作');
        $method = $_POST['method'] ?? null;
        if(!in_array($method, ['new', 'edit'])) {
            $error = '无效的数据类型';
        } else {
            $edit_id = $_POST['edit_id'] ?? null;
            $name = $_POST['children_name'] ?? null;
            $type_id = $_POST['type_id'] ?? null;
            $sort = $_POST['sort'] ?? 0;

            $data = ['children_name' => $name,'type_id' => $type_id, 'create_time' => time(),'sort' => $sort];
            if($method == 'edit' && $edit_id) {
                $DB->update('exchange_type_children', $data, ['id'=>$edit_id]);
                operate_log('exchange_type_children', 'exchange_type_children', [
                    '类型子类ID' => $edit_id
                ]);
                $success = '子类修改成功';
            } else {
                $DB->insert('exchange_type_children', $data);
                $id = $DB->id();
                if (!$id) $error = '子类类型失败';
                else {
                    $success = '添加子类成功';
                    operate_log('exchange_type_children', 'new_item', [
                        '类型子类ID' => $id,'名称' => $name
                    ]);
                }
            }
        }
    }
    else if ($action == 'godown') {
        checkFunc(3, '权限不足，无法操作');
        $aid = $_GET['id'] ?? null;
        if (!$aid) $error = '无效的类型ID';
        else {
            $DB->update('exchange_type_children', ['status'=>0], ['id' => $aid]);
            $success = '下架成功';
            operate_log('exchange_type_children', 'exchange_type_children', ['子类ID' => $aid]);
        }
    }
    else if ($action == 'goup') {
        checkFunc(3, '权限不足，无法操作');
        $aid = $_GET['id'] ?? null;
        if (!$aid) $error = '无效的类型ID';
        else {
            $DB->update('exchange_type_children', ['status'=>1], ['id' => $aid]);
            $success = '上架成功';
            operate_log('exchange_type_children', 'exchange_type_children', ['子类ID' => $aid]);
        }
    }
    else {
        $error = '无效的操作';
    }
    goExit('exchange_children.php?success=' . $success . '&error=' . $error.'&types='.$search_type);
}

display_header();

$types = $_GET['types'] ?? null;
$item_where = ['ORDER'=>['id'=>'DESC']];
if ($types){
    $item_where['type_id'] = $types;
}
$all_items = $DB->select('exchange_type_children', '*',$item_where);
if ($all_items){
    $type_ids = array_column($all_items,'type_id');
    $type_arr = $DB->select('exchange_type',['id','type_name'],['id' => $type_ids]);
    foreach ($all_items as &$z){
        $z['type_name'] = '';
        foreach ($type_arr as $v){
            if ($z['type_id'] == $v['id']){
                $z['type_name'] = $v['type_name'];
            }
        }
    }
}

$type = $DB->select('exchange_type', '*');

?>

<link href="css/jquery.dm-uploader.min.css" rel="stylesheet">
<style>
    #fileinp{
        position: absolute;
        left: 0;
        top: 0;
        opacity: 0;
    }
</style>

<section class="content">
    <?php if($error) : ?>
        <div class="alert alert-warning alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            <span><?php echo $error; ?></span>
        </div>
    <?php endif ?>
    <?php if($success) : ?>
        <div class="alert alert-success alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <h4><i class="icon fa fa-check"></i> 成功</h4>
            <?php echo $success; ?>
        </div>
    <?php endif ?>

    <div class="box box-primary">
        <div class="box-header with-border">
            <h3 class="box-title"><i class="fa fa-search"></i> 兑换子类管理</h3>
        </div>

        <div class="box-body" id="vbody">

            <button type="button" class="btn btn-primary pull-left add_children" style="margin-bottom:10px;">添加子类</button>
            <div class="">
                <div class="line">
                    <form action="exchange_children.php" method="get">
                        <div class="pull-left" style="padding:0px 15px; display:inline-block;line-height:32px;">
                            <div class="pull-left">优惠券类型:&nbsp;&nbsp;</div>
                            <select class="form-control" name="types" style="display:inline !important;float:left;width:auto;">
                                <option value=""></option>
                                <?php foreach ($type as $val){ ?>
                                    <option value="<?php echo $val['id'] ?>" <?php if ($types == $val['id']){ ?> selected <?php }?>><?php echo $val['type_name']; ?></option>
                                <?php }?>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary pull-left">查看数据</button>
                    </form>
                </div>
            </div>
            <table id="logtable" class="table table-bordered table-striped dataTable table-hover " role="grid" aria-describedby="example1_info">
                <thead>
                <tr>
                    <th class="" rowspan="1" colspan="1" width="100" style="text-align:center">ID</th>
                    <th class="" rowspan="1" colspan="1" width="150">排序</th>
                    <th class="" rowspan="1" colspan="1" width="150">类型名称</th>
                    <th class="" rowspan="1" colspan="1" width="150">子类名称</th>
                    <th class="" rowspan="1" colspan="1" width="150">操作时间</th>
                    <th class="" rowspan="1" colspan="1" style="text-align:center">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php if(!empty($all_items)) { foreach($all_items as $item) { ?>
                    <tr role="row" style="text-align:center;">
                        <td class="item_id"><?php echo $item['id']; ?></td>
                        <td class="item_sort"><?php echo $item['sort']; ?></td>
                        <td class="type_name" typeid="<?php echo $item['type_id']; ?>" style="text-align:left;font-size:16px;"><?php echo $item['type_name']; ?></td>
                        <td class="children_name" style="text-align:left;font-size:16px;"><?php echo $item['children_name']; ?></td>
                        <td><?php echo date('Y-m-d H:i:s',$item['create_time']); ?></td>
                        <td >
                            <button type="button" class="btn btn-sm btn-success edit_children">修改</button> |
                            <?php if($item['status']==1) { ?><button type="button" class="btn btn-sm btn-warning godown" iid="<?php echo $item['id']; ?>">下架</button> |<?php } ?>
                            <?php if($item['status']==0) { ?><button type="button" class="btn btn-sm btn-success goup" iid="<?php echo $item['id']; ?>">上架</button> |<?php } ?>
                            <button type="button" class="btn btn-sm btn-danger del_notice" iid="<?php echo $item['id']; ?>">删除</button>
                        </td>
                    </tr>
                <?php } } ?>
                </tbody>
            </table>

            <!-- 添加兑换类型 -->
            <div class="modal fade" id="modal-add-admin">
                <div class="modal-dialog modal-lg">
                    <form id="item_form" action="exchange_children.php?action=add_children" method="post">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title"></h4>
                            </div>
                            <div class="modal-body">
                                <input type="hidden" id="new_method" name="method" value="new" />
                                <input type="hidden" id="edit_id" name="edit_id" value="" />
                                <input type="hidden" id="search_type" name="search_type" value="<?php echo $types; ?>">
                                <table border="0" width="100%" class="modal_table_style">
                                    <tr height="45">
                                        <th width="150" style="text-align:right;padding-right:10px;">类型:</th>
                                        <td>
                                            <select class="form-control" id="type_id" name="type_id" style="width:100%;">
                                                <!--                                                <option value=""></option>-->
                                                <?php foreach($type as $t) : ?>
                                                    <option value="<?php echo $t['id']; ?>"><?php echo $t['id'] . ' - ' . $t['type_name']; ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr height="45">
                                        <th style="text-align:right;padding-right:10px;">名称:</th>
                                        <td><input type="text" class="form-control" size="20" name="children_name" id="children_name" /></td>
                                    </tr>
                                    <tr height="45">
                                        <th style="text-align:right;padding-right:10px;">排序:</th>
                                        <td><input type="text" class="form-control" size="20" name="sort" id="sort" value="0" /></td>
                                    </tr>
                                </table>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>
                                <button type="button" class="btn btn-primary submit_form">确定</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

        </div>

    </div>
</section>

<!-- jQuery 3 -->
<script src="js/jquery.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="js/pace.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- Select2 -->
<script src="bower_components/select2/dist/js/select2.full.min.js"></script>
<!-- InputMask -->
<script src="plugins/input-mask/jquery.inputmask.js"></script>
<script src="plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
<script src="plugins/input-mask/jquery.inputmask.extensions.js"></script>

<script src="js/jquery.dm-uploader.min.js"></script>

<script type="text/javascript">

    $(function() {
        $('.add_children').click(function() {
            $('.modal-title').text('添加子类');
            $('#children_name').val('');
            $('#edit_id').val('');
            $('#type_id').val('');
            $("#type_id option").each(function() {
                if($(this).text().length < 1) {
                    $(this).attr('selected', true);
                }
            });
            $('#modal-add-admin').modal({
                backdrop: 'static',
            });
        });

        $('.edit_children').click(function() {
            $('.modal-title').text('修改子类');
            $('#new_method').val('edit');
            var id = $(this).parent().parent().find('.item_id').text();
            var name = $(this).parent().parent().find('.children_name').text();
            var sort = $(this).parent().parent().find('.item_sort').text();
            $('#children_name').val(name);
            $('#sort').val(sort);
            var type_id = $(this).parent().parent().find('.type_name').attr('typeid');
            $("#type_id option").each(function() {
                if($(this).val() == type_id) {
                    $(this).attr('selected', true);
                }
            });
            $('#edit_id').val(id);
            $('#modal-add-admin').modal({
                backdrop: 'static',
            });
        });

        $('.submit_form').click(function() {
            $('#item_form').submit();
        });

        //下架
        $('.godown').click(function() {
            var iid = $(this).attr('iid');
            var search_type = $('#search_type').val();
            location.href = 'exchange_children.php?action=godown&id=' + iid+'&search_type='+search_type;
        });
        //上架
        $('.goup').click(function() {
            var iid = $(this).attr('iid');
            var search_type = $('#search_type').val();
            location.href = 'exchange_children.php?action=goup&id=' + iid+'&search_type='+search_type;
        });

        $('.del_notice').click(function() {
            if(confirm('确认删除该类型子类吗？\n\n！！！！！ 该操作不可恢复 ！！！！！\n\n')) {
                var iid = $(this).attr('iid');
                var search_type = $('#search_type').val();
                location.href = 'exchange_children.php?action=del&id=' + iid+'&search_type='+search_type;
            }
        });

        setTimeout(function() {
            $('.alert-success').fadeOut();
            if($('.alert-warning').find('span').html() && $('.alert-warning').find('span').html().length < 60) {
                $('.alert-warning').fadeOut();
            }
        }, 2000);
    });
</script>

</body>
</html>

